package com.orhon.smartcampus.modules.systemctl.fetcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import com.orhon.smartcampus.modules.base.mm.LangMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.orhon.smartcampus.basicComponents.Result;
import com.orhon.smartcampus.modules.base.gqlModel.GQLPageParam;
import com.orhon.smartcampus.modules.base.mm.EnvArgsMapper;
import com.orhon.smartcampus.modules.systemctl.dao.OrgDepartmentUserRelationDao;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartmentUserRelation;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDepartments;
import com.orhon.smartcampus.modules.systemctl.entity.OrgDuties;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartment;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDepartmentUsers;
import com.orhon.smartcampus.modules.systemctl.gqlModel.GQLDuty;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DepartmentUserMapper;
import com.orhon.smartcampus.modules.systemctl.mm.DutiesMapper;
import com.orhon.smartcampus.modules.teacher.dao.TInformationDao;
import com.orhon.smartcampus.modules.teacher.entity.TInformation;
import com.orhon.smartcampus.modules.user.dao.TokenService;
import com.orhon.smartcampus.modules.user.entity.Users;
import com.orhon.smartcampus.modules.user.gqlModel.GQLUser;
import com.orhon.smartcampus.modules.user.mm.UserMapper;
import com.orhon.smartcampus.utils.ObjectToMap;
import com.orhon.smartcampus.utils.Util;

import graphql.schema.DataFetcher;
import graphql.schema.DataFetchingEnvironment;

@Component
public class DepartmentUsersFetcher implements DataFetcher {


    @Autowired
    OrgDepartmentUserRelationDao orgDepartmentUserRelationDao;

    @Autowired
    TInformationDao tInformationDao;

    @Autowired
    TokenService tokenService;

    /**
     * 获取部门列表
     *
     * @param environment
     * @return
     * @throws Exception editor : ths
     */
    @Override
    public Object get(DataFetchingEnvironment environment) throws Exception {

        List<GQLDepartmentUsers> gqlDepartmentUsers = new ArrayList<>();

        GQLPageParam page = EnvArgsMapper.MAPPER.fromArgs(environment);
        String hql = "from OrgDepartmentUserRelation where deleted_at = null ";

        if (environment.containsArgument("condition")) {
            HashMap<String, Object> map = ObjectToMap.to(environment.getArgument("condition"));
            if (map.get("record_id") != null) {
                hql += "and id " + Util.decide(map.get("record_id"));
            }
            if (map.get("user_id") != null) {
                hql += "and user_id " + Util.decide(map.get("user_id"));
            }
            if (map.get("duty_id") != null) {
                hql += "and duty_id " + Util.decide(map.get("duty_id"));
            }
            if (map.get("department_id") != null) {
                hql += "and department_id " + Util.decide(map.get("department_id"));
            }
            if (map.get("is_leaders") != null) {
                hql += "and is_leaders " + Util.decide(map.get("is_leaders"));
            }
            if (map.get("leave_at") != null) {
                hql += "and leave_at " + Util.decide(map.get("leave_at"));
            }

        }
        List<OrgDepartmentUserRelation> list = orgDepartmentUserRelationDao.find(hql, null, page.getPage(), page.getLimit());


        for (OrgDepartmentUserRelation item : list) {
            GQLDepartment gqlDepartment = null;
            if (environment.getSelectionSet().contains("data/department")) {
                OrgDepartments parent = item.getSystemctlOrgDepartmentsByDepartmentId();
                gqlDepartment = DepartmentMapper.MAPPER.toOrgDepartments(parent);
            }
            GQLDuty gqlDuty = null;
            if (environment.getSelectionSet().contains("data/duty")) {
                OrgDuties orgDuties = item.getSystemctlOrgDutiesByDutyId();
                gqlDuty = DutiesMapper.MAPPER.toOrgDuties(orgDuties);
            }
            GQLUser gqluser = null;
            if (environment.getSelectionSet().contains("data/user")) {
                Users user = item.getUserUsersByUserId();
                gqluser = UserMapper.MAPPER.mapUser(user);
            }
            String user_name = null;
            if (environment.getSelectionSet().contains("data/user_name")) {
                List<TInformation> tInformation = tInformationDao.find("from TInformation where deleted_at = null and user_id = "+item.getUserUsersByUserId().getId(),null,1,1);
                if(tInformation.size()>0){
                    user_name = tInformation.get(0).getTeacherName();
                }
            }
            GQLDepartmentUsers departmentUsers = DepartmentUserMapper.MAPPER.toOrgDepartmentUserRelation(item);
            departmentUsers.setDepartment(gqlDepartment);
            departmentUsers.setDuty(gqlDuty);
            departmentUsers.setUser(gqluser);
            departmentUsers.setUser_name(LangMapper.MAPPER.mapLang(user_name));
            gqlDepartmentUsers.add(departmentUsers);
        }

        Long total = 0L;
        if (environment.getSelectionSet().contains("total")) {
            total = orgDepartmentUserRelationDao.getTotalCount(hql);
        }
        return Result.ok().put("data", gqlDepartmentUsers).put("total", total);
    }
}
